angular.module('dept.services', [])
.factory('deptService', function () {
	var deptTree;
	var createTree = function(pId){
		// 查询是否在数组中直接用下句
		var cursor = Departments.find({'parent_id': pId});
		var arr = [];
		cursor.forEach(function (obj) {
			if(obj._id){ 
				obj.children = createTree(obj._id);
	    	} 
	    	arr.push(obj); 
	    });
	    return arr; 
	};
	// 展开全部
	var expendAll = function(tree){
	   	tree.forEach(function(item) {
	   		item.$$isExpend = true;
	   		if(item.children.length){
	   			expendAll(item.children);
	   		}
	   	});
	};

	var service = {};
	service.expendAll = expendAll;
	service.getCatTree = function(pId){
		return createTree(pId);
	};

	return service;
});
